package hibernateDAOS;


import hibernate.HibernateUtil;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;


import beans.Mesa;
import beans.Mozo;

public class MesaDAO {
	
	private static MesaDAO instancia = null;
	private static SessionFactory sf = null;

	public static MesaDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new MesaDAO();
		} 	
		return instancia;
	}
	
	public void guardarMesa(Mesa m){
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(m);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void eliminarMesa(Mesa m){
		Session session = sf.openSession();
		session.beginTransaction();
		session.delete(m);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void modificarMesa(Mesa m){
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(m);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}
	@SuppressWarnings("unchecked")
	public Mesa obtenerMesa(Mesa m){
		Session session = sf.openSession();
		session.load(m, m.getnroMesa());
		session.close();
		return m;
	}
	@SuppressWarnings("unchecked")
	public List<Mesa> obtenerMesas(){
		Session session = sf.openSession();
		Query q = session.createQuery("from Mesa");
		List<Mesa> mesas = q.list();
		session.close();
		return mesas;
	}
	@SuppressWarnings("unchecked")
	public List<Mesa> obtenerMesasLibres(){
		Session session = sf.openSession();
		Query q = session.createQuery("from Mesa where grupo=-1 ");
		List<Mesa> mesas = q.list();
		session.close();
		return mesas;
	}
	
}

